<?php
namespace App\Http\Controllers\AppApi\My;

use App\Services\Admin\CompanyService;
use App\Utilities\Upload;
use Illuminate\Http\Request;

class CompanyController extends BaseController
{
    /**
     * 员工接口控制器
     * 制作者：九哥
     */

    protected $service;

    public function __construct()
    {
        parent::__construct();
        $this->service = new CompanyService();
    }

    /**
     * 基本信息
     */
    public function getBasicInfo(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token || !in_array($type,[1,2,3,4])) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->service->getOneByUid($uid);
        if (!$model) { return $this->toJson(-3); }
        $facTypeName = $this->service->getFacTypeName($model->facType);
//        $sexName = $model->legal_sex==1 ? '男' : '女';
        $dataArr = array(
            'facTypeName' => $facTypeName,
            //法人信息
            'legalName' => $model->legal_name,
            'legalSex' => $model->legal_sex,
            'legalMobile' => $model->legal_mobile,
            'legalIdcard' => $model->legal_idcard,
            'images' => $model->images,
            //公司信息
            'comName' => $model->com_name,
            'comTel' => $model->com_tel,
            'comFax' => $model->com_fax,
            'comAddress' => $model->com_address,
            'comCity' => $model->com_city,
            'cusLike' => $model->cus_like,
            'quality' => $model->quality,
            'pact' => $model->pact,
            'outType' => $model->outType,
            'ageRange' => $model->age_range,
            'detail' => $model->detail,
        );
        return $this->toJson(0,$dataArr);
    }

    /**
     * 公司认证
     */
    public function getFile(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token || !in_array($type,[1,2,3,4])) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->service->getOptionLicenceArr($uid);
        if (!isset($model['legals']) && !isset($model['coms'])) {
            return $this->toJson(-3);
        }
        $dataArr = $model;
        return $this->toJson(0,$dataArr);
    }

    /**
     * 产品
     */
    public function getProduct(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token || !in_array($type,[1,2,3,4])) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->service->getOptionPro($uid);
        if (!isset($model['zhen']) && !isset($model['suo'])
            && !isset($model['bao'])) {
            return $this->toJson(-3);
        }
        $dataArr = $model;
        return $this->toJson(0,$dataArr);
    }

    /**
     * 产品风格
     */
    public function getStyle(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token || !in_array($type,[1,2,3,4])) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $models = $this->service->getOptionStyle($uid);
        if (!$models) {
            return $this->toJson(-3);
        }
        $dataArr = array();
        foreach ($models as $k=>$model) {
            $dataArr[$k]['name'] = $model['name'];
            $dataArr[$k]['check'] = $model['check'];
        }
        return $this->toJson(0,$dataArr);
    }

    /**
     * 产品款式
     */
    public function getDesign(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token || !in_array($type,[1,2,3,4])) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $models = $this->service->getOptionDesign($uid);
        if (!$models) {
            return $this->toJson(-3);
        }
        $dataArr = array();
        foreach ($models as $k=>$model) {
            $dataArr[$k]['name'] = $model['name'];
            $dataArr[$k]['num'] = $model['num'];
        }
        return $this->toJson(0,$dataArr);
    }

    /**
     * 设备
     */
    public function getEquip(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token || !in_array($type,[1,2,3,4])) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $models = $this->service->getOptionEquip($uid);
        if (!$models) {
            return $this->toJson(-3);
        }
        $dataArr = array();
        foreach ($models as $k=>$model) {
            if ($model[4]) {
                $dataArr[$k]['name'] = $this->service->getEquipById($model[0]);
                $dataArr[$k]['brand'] = $this->service->getEquipById($model[1]);
                $dataArr[$k]['year'] = $this->service->getEquipById($model[2]);
                $dataArr[$k]['num'] = $this->service->getEquipById($model[3]);
            }
        }
        return $this->toJson(0,$dataArr);
    }

    /**
     * 产量产能
     */
    public function getYield(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token || !in_array($type,[1,2,3,4])) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $models = $this->service->getOptionYield($uid);
        if (!$models) {
            return $this->toJson(-3);
        }
        $dataArr = $models;
        return $this->toJson(0,$dataArr);
    }

    /**
     * 环境
     */
    public function getEnv(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token || !in_array($type,[1,2,3,4])) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        if ($type==3) {
            $envs = $this->service->getFacEnv($uid);
        } else {
            $envs = $this->service->getComEnv($uid);
        }
        if (!$envs) {
            return $this->toJson(-3);
        }
        $dataArr = $envs;
        return $this->toJson(0,$dataArr);
    }
}